package GuiClasses;

/**
 * TCSS 360 - Project
 */

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import objectClasses.User;
/**
 *  Panel for entering login information
 *  
 *  @author Abraham Santini
 */

public class LoginPanel extends Observable{
	private JPanel my_panel;
	private final Dimension my_text_size = new Dimension(20, 100); 
	
	/**
	 * public constructor for creating a login panel
	 */
	public LoginPanel(final Observer the_o_er) {
		my_panel = new JPanel();
		addObserver(the_o_er);
		my_panel.setLayout(new BoxLayout(my_panel, BoxLayout.Y_AXIS));
		my_panel.add(Box.createVerticalGlue());
		
		//creates the userNamePanel
		JPanel userNamePanel = new JPanel();
		userNamePanel.setLayout(new BoxLayout(userNamePanel, BoxLayout.X_AXIS));
		userNamePanel.add(Box.createHorizontalGlue());
		userNamePanel.add(new JLabel("UserName"));
		final JTextField nameField = new JTextField();
		nameField.setPreferredSize(my_text_size);
		userNamePanel.add(nameField);
		userNamePanel.add(Box.createHorizontalGlue());
		my_panel.add(userNamePanel);
		
		//creates the passwordPanel
		JPanel passwordPanel = new JPanel();
		passwordPanel.setLayout(new BoxLayout(passwordPanel, BoxLayout.X_AXIS));
		passwordPanel.add(Box.createHorizontalGlue());
		passwordPanel.add(new JLabel("Password"));
		//final JPasswordField passwordField = new JPasswordField();
		//passwordField.setPreferredSize(my_text_size);
		//passwordPanel.add(passwordField);
		final JTextField passWordField = new JTextField();
		passwordPanel.add(passWordField);
		passwordPanel.add(Box.createHorizontalGlue());
		my_panel.add(passwordPanel);
		
		//create the login button
		JButton loginButton = new JButton("Login");
			// creates the actionListener for the Login button
		loginButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				//StringBuilder sb = new StringBuilder();
				//char tempArray[] = passwordField.getPassword();
				//for(int i = 0; i < tempArray.length; i++) {
				//	sb.append(tempArray[i]);
				//}
				//System.out.println(sb.toString());
				System.out.println(passWordField.getText());
				
				//User temp = User.login(nameField.getText(),  sb.toString());
				User temp = User.login(nameField.getText(), passWordField.getText());
				if (temp != null) {
				GuiMain.setUser(temp);
				setChanged();
				notifyObservers(new AttendeePanel(the_o_er).getPanel());
				} else {
					System.out.println("User info incorrect.  Try again");
				}
				
				
				
			}
			
		});
		
		my_panel.add(loginButton);	
		//creates the registerPanel
		JPanel registerPanel = new JPanel();
		registerPanel.setLayout(new BoxLayout(registerPanel, BoxLayout.X_AXIS));
		registerPanel.add(Box.createHorizontalGlue());
		registerPanel.add(new JLabel("Need to Register?"));
		JButton registerButton = new JButton("Register");
		registerPanel.add(registerButton);
		// creates the action listener for the register button
		registerButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				setChanged();
				notifyObservers(new RegistrationPanel(the_o_er).getPanel());
				
			}
			
		});
		registerPanel.add(Box.createHorizontalGlue());
		my_panel.add(registerPanel);
		my_panel.add(Box.createVerticalGlue());
		
		
	}// end Login Panel Constructor
	/**
	 * Return the panel.
	 * @return my_panel The panel that contains the login information
	 */
	public JPanel getPanel() {
		return my_panel;
	}

}
